<template>
  <!-- // 分配角色 -->
  <div>
    <el-checkbox-group v-model="roleIds">
      <el-checkbox v-for="item in list" :key="item.id" :label="item.id">{{ item.name }}</el-checkbox>
    </el-checkbox-group>
    <div style="margin-top: 20px; text-align: right">
      <el-button type="primary" @click="hSave">确定</el-button>
      <el-button>取消</el-button>
    </div>
  </div>
</template>
<script>
import { getRoles, assignRoles } from '@/api/setting'
import { getUserDetailById } from '@/api/user'
export default {
  props: {
    curId: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      roleIds: [],
      list: []
    }
  },
  created() {
    this.loadRoles()
    this.loadRolesById()// 根据当前角色，选出当前已具备的角色
  },
  methods: {
    async hSave() {
      await assignRoles({ id: this.curId, roleIds: this.roleIds })

      this.$emit('success')
    },
    async loadRolesById() {
      const res = await getUserDetailById(this.curId)
      this.roleIds = res.data.roleIds
    },
    async loadRoles() {
      const res = await getRoles()
      console.log(res.data.rows)
      this.list = res.data.rows
    }

  }
}
</script>
